<template>
  <div>
    <el-table :data="tableData" stripe style="width: 100%" max-height="600">
      <el-table-column prop="orderBy" label="排序" width="60">
      </el-table-column>
      <el-table-column prop="name" label="名称" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="scope" label="校区范围" show-overflow-tooltip>
        <template slot-scope="scope">
          <p>{{ schoolScoped(scope.row) }}</p>
        </template>
      </el-table-column>
      <el-table-column prop="extFiled" label="业务范围" show-overflow-tooltip>
        <template slot-scope="scope">
          <p>{{ scope.row.extFiled | auditType }}</p>
        </template>
      </el-table-column>
      <el-table-column prop="des" label="描述" show-overflow-tooltip>
        <template slot-scope="scope">
          <p class="sheng">{{ scope.row.des || '--' }}</p>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="发布状态" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="1"
            inactive-value="0"
            @change="handleStatus(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180">
      </el-table-column>
      <el-table-column label="操作" width="320">
        <template slot-scope="{ row }">
          <el-button-group>
            <el-button
              v-if="row.extFiled <= 10"
              type="success"
              size="mini"
              @click="handleScore(row)"
            >
              评分项管理
            </el-button>
            <el-button type="success" size="mini" @click="handleNode(row)">
              节点管理
            </el-button>
            <el-button type="primary" size="mini" @click="handleEdit(row)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="handleDelete(row)"
              >删除</el-button
            >
          </el-button-group>
        </template>
      </el-table-column>
      <div slot="empty" class="emptyBg">
        <el-empty :image-size="200"></el-empty>
      </div>
    </el-table>
    <Pagination
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
  </div>
</template>
<script>
import {
  flowDefinitionQuery,
  flowDefinitionDelete,
  flowDefinitionUpdate,
} from '@/api/admin/audit/flowDefinition.js'
import { setSession } from '@/utils/session'
import Bus from '@/utils/bus.js'
export default {
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        scope: this.scope,
      },
      tableData: [],
      pageObj: {},
    }
  },
  props: ['scope'],
  computed: {
    schoolScoped() {
      return function (v) {
        if (v && v.sysDeptList) {
          return v.sysDeptList.map((v) => v.name).toString()
        } else {
          return '--'
        }
      }
    },
  },
  watch: {
    scope(v) {
      this.listQuery.scope = v
      this.getList()
    },
  },
  mounted() {
    this.getList()
    Bus.$on('refreshFlowList', (v) => {
      if (v) {
        Object.assign(this.listQuery, v)
      }
      this.getList()
    })
  },
  beforeDestroy() {
    Bus.$off('refreshFlowList')
  },
  methods: {
    getList() {
      // console.log(this.scope);
      // this.listQuery.scope = this.scope;
      flowDefinitionQuery(this.listQuery).then((res) => {
        this.tableData = res.data.data
        this.pageObj = res.data
      })
    },
    //节点管理
    handleNode(row) {
      setSession('flowData', JSON.stringify(row))
      this.$router.push({ name: 'flow-node' })
    },
    //评分项管理
    handleScore(row) {
      setSession('flowData', JSON.stringify(row))
      this.$router.push({ name: 'experts-score' })
    },
    //发布
    handleStatus(v) {
      let { id, status } = v
      flowDefinitionUpdate({ id, status }).then((res) => {
        this.getList()
        if (res.data.code != 0) return
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
    //编辑
    handleEdit(row) {
      Bus.$emit('showDialogFlowAdd', row)
    },
    //删除确认
    handleDelete(row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.delete(row)
        })
        .catch(() => {})
    },
    //删除
    delete(row) {
      flowDefinitionDelete(row.id).then((res) => {
        if (!res || res.data.code != 0) return
        this.getList()
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
  },
}
</script>